   $(function($){
	reloadOrganizedData();
	if(!getLocalStorageDataBykey("organizedData")) return;
	baiscData = new organizedData();
	baiscData.init(eval('(' + getLocalStorageDataBykey("organizedData") + ')'));

	//基础数据
	function organizedData(){
		//树数据编码  Code ParentCode OrganizedCode  name
		this.root = {Code: "370000000000",ParentCode:"-1", OrganizedCode:"370000000000",Name:"山东省公安厅"};//根节点 默认
		this.policationRoot = {Code: "370000270000"};											//省厅反恐总队的根节点
		this.nodeList = [];																		//当前所有节点数据		
		this.count = 0;																			//指针，递归数据源使用
		this.policationNodeList = [];															//适配dtree插件的数据
		this.abnormalData = ["371523480000","371523490000","370889000000","371209960000"];		//异常数据，编码和父编码不在一个编码下，导致树结构生成不完整
		this.adapterData = ["371599140001","371599140002","370881620001","371299100001"];		//适配数据，将此编码适配到父编码下，生成完整的树数据结构口
		this.filterData = ["0","-1","000"];														//过滤数据，过滤到数据库垃圾数据的节点
		this.issuedRoot;																		//适配dtree的根节点
		this.init = function(data){
			if(!data || data.length == 0) return;
			this.adapter(data);										//适配器
			this.sort();											//排序 ，为了递归方便，减少递归次数
			this.parseDataTree();									//解析生成树结构
			this.freeMemory();										//释放指针
		};
		this.adapter = function(data){
			for(var k in data){
				if(this.filter(data[k])) continue;
				this.nodeList.push(this.convertDBDataToTreeData(data[k]));
			}
		};
		this.filter = function(obj){
			return this.filterData.indexOf(obj["dm"]) > -1;
		};
		this.convertDBDataToTreeData = function(obj){            //将数据库的数据字段转换成本对象中节点对象的字段
			var node = new Object();
			node["OrganizedCode"] = obj["dm"];
			var index = this.abnormalData.indexOf(obj["dm"]);
			if(index > -1) {
				node["Code"] = this.adapterData[index];
			}else{
				node["Code"] = obj["dm"];
			}
			node["Name"] = obj["mc"];
			node["ParentCode"] = obj["fdm"];
			return node;
		};
		this.parseDataTree = function(){						//递归生成树结构数据，根节点为this.root;
			this.root["childrenNodes"] = getChildrenData(this.root);
		};
		this.freeMemory = function(){
			this.nodeList = [];
			this.count = 0;
		};
		this.getNodeByNodeCode = function(code){
			if(code == this.policationRoot["Code"]) code = this.root["Code"];
			return getNodeByNodeCode(code, this.root);
		};
		this.getParentNodeByNode = function(node){
			if(!node) return;
			if(node["Code"] == this.root["Code"]) return this.root;
			return node["parentNode"];
		};
		this.sort = function(){
			this.nodeList.sort(this.compare("Code"));
		};
		this.compare = function(prop){
			return function(a, b){
				if(a[prop] >= b[prop]){
					return 1;
				}else{
					return -1;
				}
			};
		};
		this.getListByCode = function(code){
			return this.getListByNode(this.getNodeByNodeCode(code));
		};
		this.getTaskOrigainListByNode = function(node){
			this.resetPolicationNodeList();
			if(!node) return this.policationNodeList;
			var parentNode = this.getParentNodeByNode(node);
			this.setIssuedRoot(parentNode);
			this.add(this.issuedRoot);
			loadNodeData(node);
			if(!this.isRootNode(node)) this.getChildrenListByParentNode(this.issuedRoot);
			return this.policationNodeList;
		};
		this.isRootNode = function(node){
			return this.root["Code"] == node["Code"];
		};
		this.getChildrenListByParentNode = function(){
			for(var k in this.issuedRoot["childrenNodes"]){
				var siblingNode = this.issuedRoot["childrenNodes"][k];
				this.policationNodeList.push(this.convertNodeToDtreeNode(siblingNode));
			};
		};
		//递归取当前根节点下的所有叶子节点
		this.getListByNode = function(node){
			this.resetPolicationNodeList();
			if(!node) return this.policationNodeList;
			this.setIssuedRoot(node);
			this.add(this.issuedRoot);
			loadNodeData(this.issuedRoot);
			return this.policationNodeList;
		};
		this.setIssuedRoot = function(node){
			this.issuedRoot = node;
		};
		this.resetPolicationNodeList = function(){
			this.policationNodeList = [];
		};
		this.add = function(node){
			this.policationNodeList.push(this.convertNodeToDtreeNode(node));
		};
		this.convertNodeToDtreeNode = function(node){
			var dtreeNode = new Object();
			dtreeNode["selfid"] = node["OrganizedCode"];
			if(this.isIssuedRoot(node)){
				dtreeNode["parentid"] = "-1";
				dtreeNode["chkDisabled"] = true;
			}else{
				dtreeNode["parentid"] = node["ParentCode"];
				dtreeNode["chkDisabled"] = false;
			}
			dtreeNode["selfname"] = node["Name"];
			return dtreeNode;
		};
		this.isIssuedRoot = function(node){
			return node["Code"] == this.issuedRoot["Code"];
		};
	};
	
	function getChildrenData(root){
		var lists = [];
		for(;baiscData.count < baiscData.nodeList.length;){
			var node = baiscData.nodeList[baiscData.count];
			if(node["Code"] == root["Code"]){
				baiscData.count++;
				continue;
			}
			if(node["ParentCode"] == root["Code"]){
				baiscData.count++;
				node["parentNode"] = root;
				node["childrenNodes"] = getChildrenData(node);
				lists.push(node);
			}else{
				return lists;
			};
		}
		return lists;
	};
	
	function getNodeByNodeCode(code, root){
		if(root["OrganizedCode"] == code) return root;
		var childrenNodes = root["childrenNodes"];
		if(childrenNodes.length == 0) return;
		for(var k in childrenNodes){
			var node = childrenNodes[k];
			if(node["OrganizedCode"] == code) return node;
			var nodeData = getNodeByNodeCode(code, node);
			if(nodeData) return nodeData;
		};
	};
	
	function loadNodeData(root){
		var childrenNodes = root["childrenNodes"];
		if(childrenNodes == 0) return;
		for(var j = 0; j < childrenNodes.length; j++){
			var node = childrenNodes[j];
			baiscData.add(node);
			loadNodeData(node);
		};
	};
	
	function reloadOrganizedData(){
		var data = $("#organizedData").val();
		if(!data) return;
		if(localStorage.getItem("organizedData")) localStorage.removeItem("organizedData");
		localStorage.setItem("organizedData", data);
	};
	
	function getLocalStorageDataBykey(key){
		return localStorage.getItem(key);
	};
	

});
